package com.minelx.sequence.pattern;

import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;

import java.util.function.Consumer;

public class AdditionalConsumeMatcher<R, T> implements ReturnMatcher<R, T> {

	private final ReturnMatcher<R, T> returnMatcher;

	private final Consumer<Object> resultConsumer;

	public AdditionalConsumeMatcher(ReturnMatcher<R, T> returnMatcher,
	                                Consumer<Object> resultConsumer) {
		this.returnMatcher = returnMatcher;
		this.resultConsumer = resultConsumer;
	}

	@Override
	public boolean isMatch(T clue) {
		return returnMatcher.isMatch(clue);
	}

	@Override
	public R onMatched(T clue) {
		R result = returnMatcher.onMatched(clue);
		resultConsumer.accept(result);
		return result;
	}
}
